package connector.rmiServer.impl;

import java.rmi.RemoteException;
import java.util.HashSet;
import java.util.List;

import mediator.MBroker;
import connector.CDBConnector;
import connector.rmiServer.CentralClient;
import connector.rmiServer.TaxaClient;
import entity.Taxi;
import entity.Taxi.Status;

//import RMIServer.ServerService;
//import RMIServer.transferObjects.StatusMessage;

public class ServerServiceImpl implements connector.rmiServer.ServerService {

	private CDBConnector standby;
	private HashSet<CentralClient> centralSet;
	private HashSet<TaxaClient> taxaSet;


	public ServerServiceImpl(CDBConnector standby){
		this.standby = standby;
		centralSet = new HashSet<CentralClient>();
		taxaSet = new HashSet<TaxaClient>();
	}

	public void sendMessage(String message) throws RemoteException {
		System.out.println("Message is: " + message);
	}

//	public StatusMessage getStatusMessage() throws RemoteException{
//	return new StatusMessage();
//	}

//	public void sendStatusMessage(StatusMessage message) throws RemoteException{
//	System.out.println(message);
//	}

	public void sendTaxaInfo(int taxaiID,int driverID,String statusStr ,long time, float latitude, float longtitude) throws RemoteException {
		System.out.println("TaxaID: " + taxaiID);
		System.out.println("DriverID: " + driverID);
		System.out.println("Status: " + statusStr);

		Status status = Taxi.Status.valueOf(statusStr);
		
				standby.prepareDBSaving(taxaiID, driverID, status,time,latitude,longtitude);
		Taxi taxa = Taxi.getTaxi(taxaiID);

		for(CentralClient client : centralSet){
			client.updatePosition(taxa.getTaxiID(), taxa.getPosition().getTime().getTimeInMillis(), taxa.getPosition().getLatitude(), taxa.getPosition().getLongitude());
			client.setStatus(taxaiID, statusStr);
		}
	}

	public void registerClient(CentralClient client) throws RemoteException {
		centralSet.add(client);
		System.out.println("Central Client registered"+client);
	}

	public void registerClient(TaxaClient client) throws RemoteException {
		taxaSet.add(client);
		System.out.println("Taxa Client registered"+client);
	}

	public void placeOrder(String address, String city, String zip, String phone, long time) throws RemoteException {
		int orderID = MBroker.getInstance().orderCount()+1; 
		entity.Order aOrder = new entity.Order(orderID, address, city, zip, phone,time);
		standby.prepareOrderSaving(aOrder);
		for(TaxaClient taxaClient : taxaSet)
		{
			taxaClient.updateOrders(aOrder.getOrderid(),aOrder.getAddress(), aOrder.getCity(), aOrder.getZipCode(), aOrder.getPhone());
		}

	}

	public List<Float> getStatistics(int weeks,long chosenDate,int zipCodeFrom,int zipCodeTo) throws RemoteException {
		 List<Float> stat = null;
		try {
			stat = MBroker.getInstance().makeStatistics(weeks, chosenDate, zipCodeFrom, zipCodeTo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return stat;
	}
	

}


